package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.RestResponseBo;
import com.example.demo.model.dto.UserDto;
import com.example.demo.model.po.UserPo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * create by elitetyc
 * date：2019/6/12
 * desc：
 **/
@Service
public class ManageService {

    @Autowired
    private UserMapper userMapper;


    public int updateInfo(UserPo userPo) {
        return userMapper.updateBy(userPo);
    }

    public PageInfo<UserPo> selectAllUser(int page) {
        PageInfo<UserPo> pageInfo = PageHelper.startPage(page, 10).doSelectPageInfo(()->userMapper.selectALlUser());

        System.out.println(pageInfo);
        return pageInfo;

    }

    public int deleteUser(String account) {
        return userMapper.deleteByAccount(account);
    }

    public RestResponseBo insertUser(UserPo userPo) {
        UserPo userPo1 = userMapper.selectByAccount(userPo.getAccount());
        if (userPo1!=null){
            return RestResponseBo.fail("已存在该账户");
        }
        int res = userMapper.insertUser(userPo);
        if (res==1){
            return RestResponseBo.ok();
        }else {
            return RestResponseBo.fail("新增失败");
        }
    }


    public int deleteUserById(int userId) {
        return userMapper.deleteUserById(userId);
    }
}
